package com.gzx.spring.apis.tests.xinyisou;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzx.spring.apis.utils.xinyisou.RSATool;

import cn.hutool.http.HttpUtil;

//璞玉95S.docx
public class OpenApiDemo2 {

	/**
	 * 私钥(java使用PKCS8版本) 测试环境是统一私钥，而正式环境需要商户这边生成公私钥，上传公钥至taojin@xysl.com 生成公私钥代码，下面有
	 */
	private static final String channelPriKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALMM1nBkwBKv7jgq\n"
			+ "ErC0vAaYuI3GdT0SkIXiZziThNvq3EI14wSVsrW7FUzPlxT8iAQFJKNTHTyddnUU\n"
			+ "bGS1Ggk8kFqw2hMz4CqIMgc/h5YKrwG2zi8tYb4+xsir07MyNQyW0nP9fQgv1urB\n"
			+ "VuLvqIZ+txfq8FO8RxBqOFz5WmMPAgMBAAECgYBJdkXxXVT1FdvOjWyXtb/DkRt/\n"
			+ "h+KISTqkw+yH6PbE6Eqwm71mHylwqK6nkjCxup7vz1Bl9m7zdMOqa9PBShDA83qJ\n"
			+ "Z39FpNwoRuhbYX9jlSu8uPVh7FTfqghA7G9woOUYfOYibWg55RyziaLKlAFTYmps\n"
			+ "dGgM7YqQPkpkhIh4qQJBAOHNqb2exq3R5fKdzQDoUir0NwoGYA7vZBHSCvOLtGt5\n"
			+ "ZzN4gXoTR/VvDAJUUvqED/U+L5JaxzLWkc/TJWR4VRUCQQDK/pYopTlZDbvDSA9O\n"
			+ "2PMnHLs50WgvR/T7WEeU145ahjQBzoFTt9OeUaltbLcZSwvhQ4bWYoZXxALw7ojt\n"
			+ "LWiTAkEAnNRJBmhWubdFLIMpNW4mU6sW5yGSag0exbnK3Wi0wMirQvZ5hx4JJpAg\n"
			+ "GSYUE+bRZpXwWIgwPkEFOQD/wY5KGQJALNAvTiHLm0FQ5jeOSZXTeBr7qjP0kBuO\n"
			+ "lb5TbyR3JLzj1lJEcrruWNEmWiXZ9hd/M+e4aLOvfMik7ZN7XwDbSQJBAKJtL/UT\n"
			+ "uEy+/wmrL+LRu7QrO6wXcnK/rEru3nmHoECpnFKtad4tWJF7ky2T23JO9CiDmrhl\n" + "3h70N8l7TeWWp9I=";

	/**
	 *
	 *
	 *
	 * @throws Exception
	 */
	public static void testApiInvoke() throws Exception {
		// --------------------------------------------对接参数-----------------------------------------//
		String appId = "27a5fe187f5c52ab";// 由信易搜提供
		String token = "2567ec69b4f474be6cc0e8a8fb7a1b12";// 由信易搜提供

		String env = "debug"; // 环境参数，生产为api,测试为debug

		// --------------------------------------------业务入参-----------------------------------------//
		Map<String, Object> originParams = new HashMap<>();
		originParams.put("mobile", "13488774341");
		originParams.put("idcard", "210727197902096937");
		originParams.put("name", "吴岩");

		String serverUrl = "http://openapi.xysl.com/" + env + "/gateway/puyu_score95s";
		System.out.println(channelPriKey.replace("\n", ""));

		String orgBizStr = JSON.toJSONString(originParams);
		System.out.println("orgBizStr:" + orgBizStr);

		String biz = Base64.encodeBase64URLSafeString(RSATool.rsaEncryptByPrivateKey(
				RSATool.parseRSAPrivateKey(channelPriKey), orgBizStr.getBytes(Charset.forName("UTF-8"))));

		System.out.println("biz:" + biz);

		String url = serverUrl + "?app_id=" + appId + "&biz=" + biz;

		System.out.println("url:" + url);

		String bodyStr = HttpUtil.createGet(url).header("Authorization", token).execute().body();
		System.out.println("resBody:" + bodyStr);

		JSONObject bodyJson = JSONObject.parseObject(bodyStr);
		String code = bodyJson.getString("code");
		if ("0".equals(code)) {// 0-success
			String returnBizData = bodyJson.getString("bizData");
			final byte[] bytes = RSATool.rsaDecryptByPrivateKey(RSATool.parseRSAPrivateKey(channelPriKey),
					Base64.decodeBase64(returnBizData));
			String plainText = new String(bytes, "utf-8");
			System.out.println("invoke success,desContent:" + plainText);
		} else if ("1".equals(code)) {// 1-not hit
			String msg = bodyJson.getString("msg");
			System.out.println("invoke not hit,msg:" + msg);
		} else {
			String msg = bodyJson.getString("msg");
			System.out.println("invoke fail,msg:" + msg);
		}
	}

	/**
	 *
	 * 信易搜限制了访问IP段，所以先要提供下白名单IP列表给到信易搜
	 *
	 *
	 * 上生产流程如下 1
	 * 测试环境密钥默认使用demo中，生产环境密钥需要客户来生成，其中公钥并发送至taojin@xysl.com，公私密钥的生成方法可以参考OpenApiDemo.generateSecret()
	 * 或者 https://uutool.cn/rsa-generate/ 密钥长度选1024 格式选PKCS8 2
	 * 替换成生产的配置参数，程序中需要替换的地方有4处， app_id token url中的env从debug改成api 加密和解密的私钥使用客户生成的
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		testApiInvoke();
	}

}
